package com.ruoyi.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Important;
import com.ruoyi.system.service.IImportantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 关键站点数据Controller
 * 
 * @author zhangkai
 * @date 2021-11-24
 */
@RestController
@RequestMapping("/tietasystem/importantservice")
public class ImportantController extends BaseController
{
    @Autowired
    private IImportantService importantService;

    /**
     * 查询关键站点数据列表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:list')")
    @GetMapping("/list")
    public TableDataInfo list(Important important)
    {
        startPage();
        if(important.getVersion() != null) {
            String newDateRange = important.getVersion().replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "").substring(0,12);
            important.setVersion(newDateRange);
         }
        List<Important> list = importantService.selectImportantList(important);
        return getDataTable(list);
    }

    /**
     * 导出关键站点数据列表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:export')")
    @Log(title = "关键站点数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Important important)
    {
        List<Important> list = importantService.selectImportantList(important);
        ExcelUtil<Important> util = new ExcelUtil<Important>(Important.class);
        return util.exportExcel(list, "关键站点数据数据");
    }

    /**
     * 获取关键站点数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(importantService.selectImportantById(id));
    }

    /**
     * 新增关键站点数据
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:add')")
    @Log(title = "关键站点数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Important important)
    {
        return toAjax(importantService.insertImportant(important));
    }

    /**
     * 修改关键站点数据
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:edit')")
    @Log(title = "关键站点数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Important important)
    {
        return toAjax(importantService.updateImportant(important));
    }

    /**
     * 删除关键站点数据
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:remove')")
    @Log(title = "关键站点数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(importantService.deleteImportantByIds(ids));
    }
    
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Important> util = new ExcelUtil<Important>(Important.class);
        return util.importTemplateExcel("关键站址数据");
    }
    
    @Log(title = "关键站址数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('tietasystem:importantservice:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,String dateRange) throws Exception
    {
        ExcelUtil<Important> util = new ExcelUtil<Important>(Important.class);
        List<Important> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = importantService.importTotal(dataList, operName,dateRange);
        return AjaxResult.success(message);
    }
}
